package com.ssyx.home.service.Impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssyx.common.auth.AuthContextHolder;
import com.ssyx.home.feign.ActivityFeignClient;
import com.ssyx.home.feign.ProductFeignClient;
import com.ssyx.home.feign.UserFeignClient;
import com.ssyx.home.service.HomeService;
import com.ssyx.model.product.Category;
import com.ssyx.model.product.SkuInfo;
import com.ssyx.vo.search.SkuEsQueryVo;
import com.ssyx.vo.user.LeaderAddressVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * ClassName:HomeServiceImpl
 * Description:
 *
 * @Author kay
 * @Create 2024/5/15 16:23
 * @Version 1.0
 */
@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private ActivityFeignClient activityFeignClient;
    //首页数据显示接口
    @Override
    public Map<String, Object> homeData(Long userId) {
        //根据userId获取当前登录用户的提货地址信息,远程调用service-user
        LeaderAddressVo leaderAddressVo = userFeignClient.getUserAddressByUserId(userId);

        //获取所有分类信息,远程调用service-product
        List<Category> categoryList = productFeignClient.findAllCategoryList();

        //获取新人专享商品,远程调用service-product
        List<SkuInfo> newPersonSkuInfoList = productFeignClient.findNewPersonSkuInfoList();

        //获取爆款商品,远程调用service-search,
        //ES使用不了,暂时用产品表中,销量最高的产品代替,远程调用service-product
        List<SkuInfo> hotSkuList = productFeignClient.findMaxStockList();
        //TODO 获取用户首页秒杀数据

        //封装数据到map集合,返回
        Map<String, Object> map =new HashMap<>();
        map.put("leaderAddressVo",leaderAddressVo);
        map.put("categoryList",categoryList);
        map.put("newPersonSkuInfoList",newPersonSkuInfoList);
        map.put("hotSkuList",hotSkuList);
        return map;
    }

    //获取分类信息
    @Override
    public List<Category> getAllcategoryList() {
        List<Category> allCategoryList = productFeignClient.findAllCategoryList();
        return allCategoryList;
    }

    //分类列表中搜索商品
    @Override
    public Page<SkuInfo> getPage(Long page, Long limit, SkuEsQueryVo skuEsQueryVo) {
        Long categoryId = skuEsQueryVo.getCategoryId();
        String keyword = skuEsQueryVo.getKeyword();
        //向SkuEsQueryVo中设置wareId,当前登录用户的仓库id
        Long wareId = AuthContextHolder.getUserLoginVo().getWareId();
        skuEsQueryVo.setWareId(wareId);

        //判断keyword是否为空,
        Page<SkuInfo> skuInfoPage = null;
        if(StringUtils.isEmpty(keyword)){
            //如果为空,根据仓库id+分类id查询,远程调用,service-product
            skuInfoPage = productFeignClient.findByWareIdAndCategoryId(categoryId,wareId,page,limit);
        }else{
            //如果keyword不为空,根据仓库id和keyword查询,远程调用,service-product
            skuInfoPage = productFeignClient.findByWareIdAndKeyword(keyword, wareId, page, limit);
        }
        List<SkuInfo> skuInfoList = skuInfoPage.getRecords();
        //获取sku对应的促销活动标签 Map<Long, List<String>> 中 Long是skuId, List<String>是标签的集合
        if(!CollectionUtils.isEmpty(skuInfoList)) {
            List<Long> skuIdList = skuInfoList.stream().map(skuInfo -> skuInfo.getId()).collect(Collectors.toList());
            Map<Long, List<String>> skuIdToRuleListMap = activityFeignClient.findActivity(skuIdList);
            if(null != skuIdToRuleListMap) {
                skuInfoList.forEach(skuInfo ->{
                    skuInfo.setRuleList(skuIdToRuleListMap.get(skuInfo.getId()));
                });
            }
        }

        return skuInfoPage;
    }
}
